package com.qqtech.qquan.circle.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleApplyStatusDictEnum;
import com.qqtech.qquan.circle.model.CircleApply;
import com.qqtech.qquan.circle.model.CircleApplyQuery;
import com.qqtech.qquan.circle.model.CircleApplyVo;
import com.qqtech.qquan.circle.model.CircleBase;
import com.qqtech.qquan.circle.service.CircleApplyService;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/circle/apply")
public class CircleApplyController extends BaseWebController<CircleApply, CircleApplyQuery> {
	@Resource
	private CircleApplyService circleApplyService;
	@Resource
	private UserBaseService userBaseService;
	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private CircleMemberService circleMemberService;
	
	@Override
	protected BaseService<CircleApply> getBaseService() {
		return circleApplyService;
	}
	
	/**
	 * 添加圈子申请加入
	 * @param circleInvite {circleId:XXX,msg:XXX}
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody CircleApply circleApply) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		Integer circleId = circleApply.getCircleId();
		circleApply.setApplyerId(curUserId);
		circleApplyService.apply2JoinCircle(curUserId, circleId);
		return result;
	}
	
	/**
	 * 接受加入圈子申请
	 * @param circleApply {id:xxx}
	 * @return
	 * @throws BizzException 
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/acceptApply")
	@ResponseBody
	public  Result acceptApply(@RequestBody CircleApply circleApply) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		Integer applyId = circleApply.getId();
		if(applyId==null){
			result.addErr("申请id为空！");
			return result;
		}
		
		circleApply = circleApplyService.getById(applyId);
		if(circleApply==null){
			result.addErr("找不到对应的申请记录！");
			return result;
		}
		
		if(curUserId.equals(circleApply.getCircleOwnerId()) || 
				circleMemberService.isCircleManager(curUserId, circleApply.getCircleId())){
			circleApplyService.acceptApply(circleApply);
		}else{
			result.addErr("权限不足！");
			return result;
		}
		return result;
	}
	
	/**
	 * 忽略加入圈子申请
	 * @param circleApply {id:xxx}
	 * @return
	 * @throws BizzException 
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/ignoreApply")
	@ResponseBody
	public  Result ignoreApply(@RequestBody CircleApply circleApply) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		Integer applyId = circleApply.getId();
		if(applyId==null){
			result.addErr("申请id为空！");
			return result;
		}
		
		circleApply = circleApplyService.getById(applyId);
		if(curUserId.equals(circleApply.getCircleOwnerId()) || 
				circleMemberService.isCircleManager(curUserId, circleApply.getCircleId())){
			circleApplyService.ignoreApply(circleApply);
		}else{
			result.addErr("权限不足！");
			return result;
		}
		return result;
	}
	/**
	 * 删除-加入圈子申请
	 * @param circleApply {id:xxx}
	 * @return
	 * @throws BizzException 
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/removeApply")
	@ResponseBody
	public  Result removeApply(@RequestBody CircleApply circleApply) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		Integer applyId = circleApply.getId();
		if(applyId==null){
			result.addErr("申请id为空！");
			return result;
		}
		
		circleApply = circleApplyService.getById(applyId);
		if(!curUserId.equals(circleApply.getCircleOwnerId())){
			result.addErr("权限不足！");
			return result;
		}
		
		circleApplyService.removeApply(circleApply);
		return result;
	}
	/**
	 * 我的圈子申请加入列表（我是圈主）
	 * @param circleApplyQ
	 * @return
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/circleApply2Me")
	@ResponseBody
	public  Result circleApply2Me(@RequestBody CircleApplyQuery circleApplyQ){
		Result result = new Result();
		UserBaseVo userVo= null;//用于取用户呢称、头像
		Integer curUserId = super.getUserId();//当前用户id
		circleApplyQ.setCircleOwnerId(curUserId);//我是圈主
		circleApplyQ.setStatus(CircleApplyStatusDictEnum.WAITING.getCode());
		CircleBase circle = null;
		Pageable pageKit = new PageKit(circleApplyQ.getPage(), new PageOrder(Direction.DESC, "newAt"));
		List<CircleApplyVo> applyList = circleApplyService.queryList(circleApplyQ, pageKit);
			if(applyList!=null && applyList.size()>0){
				for(CircleApplyVo applyVo:applyList){
					userVo  = userBaseService.getById(applyVo.getApplyerId());
					applyVo.setApplyerImg(userVo.getLogo());
					applyVo.setApplyerName(userVo.getNick());
					circle = circleBaseService.getById(applyVo.getCircleId());
					if(circle!=null){
						applyVo.setCircleName(circle.getName());
					}
							
				}
			}
		
		result.put("applyList", applyList);
		return result;
	}
}
